package com.example.singleton.lazy;

import java.lang.reflect.Constructor;

/**
 * 内部类的单例
 * 优点：利用了Java本身语法特点，性能高，避免了内存浪费,不能被反射破坏
 * 缺点：不优雅
 */
public class LazyStaticInnerClassSingleton {

    private LazyStaticInnerClassSingleton() {
        // 防止反射
        if (LazyHolder.INSTANCE != null) {
            throw new RuntimeException("不允许非法访问");
        }
    }

    public LazyStaticInnerClassSingleton getInstance() {
        return LazyHolder.INSTANCE;
    }

    private static class LazyHolder {
        private static final LazyStaticInnerClassSingleton INSTANCE = new LazyStaticInnerClassSingleton();
    }


    public static void main(String[] args) {
        try {
            Class clazz = LazyStaticInnerClassSingleton.class;
            Constructor c = clazz.getDeclaredConstructor();
            Object obj = c.newInstance();
            System.out.println(obj);
            System.out.println(c);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
